<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/19
 * Time: 14:15
 */

namespace App\Http\Middleware;

use App\Models\Member;
use Closure;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;

class ApiMiddleware extends Middleware
{
    public function handle($request, Closure $next)
    {
        if(!$this->checkLogin()){
            return Response()->json(['code'=>202,'msg'=>'登录已过期，请重新登录']);
        }
        return $next($request);
    }

    /**
     * 核对登录信息
     * @return bool
     */
    protected function checkLogin()
    {
        $token = Request()->header('token');
        $cacheMember = Cache::get($token);
        if(!$cacheMember){
            Member::where('session_key',$token)->update(['session_key'=>'']);
            return false;
        }
        return true;
    }
}
